/* 
 *	Purpose: Implementation of CInputController class.
 *	Notes
 *
 *	$Archive: /FormsPlayer/FormsPlayer.net/InputController.cpp $
 *	$Revision: 6 $
 *
 *	$Author: Richard.bidgood $
 *	$Date: 22/10/03 18:11 $
 *	
 *	$History: InputController.cpp $
 * 
 * *****************  Version 6  *****************
 * User: Richard.bidgood Date: 22/10/03   Time: 18:11
 * Updated in $/FormsPlayer/FormsPlayer.net
 * Memory Leaks!
 * 
 * In InitBehaviour(), allocated memory pass directly too function
 * RegisterEvent()
 * 
 * *****************  Version 5  *****************
 * User: Richard.bidgood Date: 21/10/03   Time: 14:10
 * Updated in $/FormsPlayer/FormsPlayer.net
 * Memory Leak!
 * BSTR not in CInputController::ElementReady(void) changed too CComBSTR.
 * 
 * *****************  Version 4  *****************
 * User: Phil.booth   Date: 12/08/03   Time: 11:32
 * Updated in $/FormsPlayer/FormsPlayer.net
 * Explicitly cast pFac.
 * 
 * *****************  Version 3  *****************
 * User: Paulrbutcher Date: 28/07/03   Time: 18:54
 * Updated in $/FormsPlayer/FormsPlayer.net
 * 
 * *****************  Version 2  *****************
 * User: Paulrbutcher Date: 28/07/03   Time: 17:59
 * Updated in $/FormsPlayer/FormsPlayer.net
 * merged no-viewlinks branch
 * 
 * *****************  Version 1  *****************
 * User: Phil.booth   Date: 28/07/03   Time: 16:08
 * Created in $/FormsPlayer/FormsPlayer-no-viewlinks
 * 
 */

#include "stdafx.h"
#include "InputController.h"
#include "FP_ReturnCodes.h"
#include "FP_Definitions.h"

CInputController::CInputController(void)
: m_lEventCookieValueChanged(0), m_lEventCookieValid(0), m_lEventCookieInvalid(0), m_lEventCookieReadOnly(0)
, m_lEventCookieReadWrite(0), m_lEventCookieEnabled(0), m_lEventCookieDisabled(0), m_lEventCookieHelp(0)
, m_lEventCookieHint(0)
{
}

CInputController::~CInputController(void)
{
}

HRESULT CInputController::ElementReady(void)
{
	HRESULT hr = S_OK;
	CComPtr<IHTMLElement> pEl;
	this->GetMasterElement(&pEl);
	if(pEl) {
		CComQIPtr<IHTMLElement2> pEl2 = pEl;
		if(pEl2) {

			CFactory * pFac;
			CFactory::GetFactory(&pFac);
			if(pFac)
			{
				CComVariant vtFac(reinterpret_cast<IDispatch*>(pFac));
				CComBSTR bsHTC(g_lpwszTagInput);
				hr = pEl2->addBehavior(bsHTC,&vtFac,0);
				if(SUCCEEDED(hr))
					hr = CXFormsElement::ElementReady();
			}
		}
	}
	return hr;
}

HRESULT CInputController::InitBehaviour(void)
{
	BSTR sEvent = ::SysAllocString(g_lpwszEventSourceValueChanged);
 	RegisterEvent(sEvent, &m_lEventCookieValueChanged);
	::SysFreeString(sEvent);

	sEvent = ::SysAllocString(g_lpwszEventSourceValid);
	RegisterEvent(sEvent, &m_lEventCookieValid);
	::SysFreeString(sEvent);

	sEvent = ::SysAllocString(g_lpwszEventSourceInvalid);
	RegisterEvent(sEvent, &m_lEventCookieInvalid);
	::SysFreeString(sEvent);

	sEvent = ::SysAllocString(g_lpwszEventSourceReadOnly);
	RegisterEvent(sEvent, &m_lEventCookieReadOnly);
	::SysFreeString(sEvent);

	sEvent = ::SysAllocString(g_lpwszEventSourceReadWrite);
	RegisterEvent(sEvent, &m_lEventCookieReadWrite);
	::SysFreeString(sEvent);

	sEvent = ::SysAllocString(g_lpwszEventSourceEnabled);
	RegisterEvent(sEvent, &m_lEventCookieEnabled);
	::SysFreeString(sEvent);

	sEvent = ::SysAllocString(g_lpwszEventSourceDisabled);
	RegisterEvent(sEvent, &m_lEventCookieDisabled);
	::SysFreeString(sEvent);
	
	sEvent = ::SysAllocString(g_lpwszEventSourceHelp);
	RegisterEvent(sEvent, &m_lEventCookieHelp);
	::SysFreeString(sEvent);

	sEvent = ::SysAllocString(g_lpwszEventSourceHint);
	RegisterEvent(sEvent, &m_lEventCookieHint);
	::SysFreeString(sEvent);

	return CXFormsElement::InitBehaviour();
}
